<template>
	<view class="config-container">
		<view class="my-label">
			<view class="label-header">
				<text>我的标签</text>
				<text class="edit" @click="edit">{{is_edit?'完成':'编辑'}}</text>
			</view>
			<view class="label-content">
				<view class="label-item" v-for="(item,index) in my_label" :key="item._id"
					@click="del_label(index)">
					{{item.name}}
					<uni-icons type="minus" color="#f00" size="20" v-show="is_edit" />
				</view>
			</view>
			<view class="no-data" v-show="!my_label.length">
				当前没有数据
			</view>
		</view>
		<view class="recommend-label">
			<view class="label-header">
				<text>推荐标签</text>
			</view>
			<view class="label-content">
				<view class="label-item" v-for="item in recommend_label" :key="item._id"
					@click="add_label(item)">
					{{item.name}}
					<uni-icons type="plus" color="#67c23a" size="20" v-show="is_edit" />
				</view>
			</view>
			<view class="no-data" v-show="!recommend_label.length">
				当前没有数据
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex'
	export default {
		data() {
			return {
				is_edit: false,
				label_list: []
			}
		},
		computed: {
			...mapState(['my_label']),
			recommend_label() {
				return this.label_list
					.filter(item => !this.my_label.map(item => item._id).includes(item._id))
			}
		},
		async onLoad() {
			this.label_list = await this.$http.get_label()
		},
		methods: {
			del_label(index) {
				if (this.is_edit) {
					this.my_label.splice(index, 1)
				}
			},
			add_label(label) {
				if (this.is_edit) {
					this.my_label.push(label)
				}
			},
			edit() {
				if (this.is_edit) {
					this.$store.commit('set_my_label', this.my_label)
				}
				this.is_edit = !this.is_edit
			}
		}
	}
</script>

<style lang="scss">
	.my-label,
	.recommend-label {
		// background-color: #fff;
		min-height: 300rpx;

		.label-header {
			@include flex();
			font-size: 28rpx;
			padding: 15rpx 20rpx;
			border-bottom: 1px solid #999;

			.edit {
				color: #30b33a;
			}
		}

		.label-content {
			@include flex(flex-start, row, wrap);

			.label-item {
				padding: 8rpx 15rpx;
				border: 1px solid;
				color: #8dc6ff;
				border-radius: 10rpx;
				font-size: 27rpx;
				margin: 24rpx 16rpx 0;
				position: relative;

				.uni-icons {
					position: absolute;
					top: -20rpx;
					right: -20rpx;
					background-color: #fff;
					border-radius: 50%;
				}
			}
		}
	}
</style>
